<!--
 * @Author: ningjz
 * @Date: 2022-04-11 18:05:46
 * @Description: CPI与酒店月均价
-->
<template>
  <com-box :title="BarState.title">
    <!-- <template #titleLeft> -->
    <!-- <div class="header-left header-box">{{ BarState.unit }}</div> -->
    <!-- </template> -->
    <template #titleRight>
      <el-icon
        class="item-icon rightTop"
        :class="{ active: isTransition }"
        @click="handIconClick"
      >
        <ZoomIn />
      </el-icon>
    </template>
    <template v-slot>
      <div class="nav">
        <div class="nav-warnLine"></div>
        <div class="box-bar echart-item">
          <charts :params="BarState.params"></charts>
        </div>
      </div>
    </template>
  </com-box>
</template>
<script lang="ts">
import comBox from '@/components/comBox/comBox.vue'
import { defineComponent, reactive } from 'vue'
import httpRequest from '@/service'
import { ZoomIn } from '@element-plus/icons-vue'

export default defineComponent({
  name: 'rightLower',
  components: {
    comBox,
    ZoomIn
  },
  props: {
    goodsId: {
      type: String,
      default: ''
    },
    isTransition: {
      type: Boolean,
      default: false
    }
  },
  emits: ['enlarge'],
  setup(props, { emit }) {
    //折线图
    const BarState: any = reactive({
      title: '',
      isGroup: false,
      unit: '',
      params: {
        height: '379px',
        loading: true,
        forceRedraw: true, // 强制重绘
        map: {},
        options: {
          dataset: {
            dimensions: [],
            source: []
          },
          legend: {
            show: true,
            right: 0,
            top: 3,
            itemWidth: 15,
            width: '70%',
            itemStyle: {
              decal: {
                symbol: ''
              }
            }
          },
          // 待完善：此处如果注释会导致添加不上axisLabel->fontSize的chartFontSize，因为递归合并时直接赋值xAxis全局配置了
          xAxis: [{}],
          yAxis: [
            {
              min: '60',
              splitLine: {
                show: false
              },
              name: ['']
            }
          ],
          grid: {
            top: 30,
            bottom: 49,
            right: 0
          },
          dataZoom: [
            {
              show: true,
              type: 'slider',
              start: 0,
              end: 100,
              width: '96%',
              left: '3%',
              bottom: 22,
              height: 15,
              borderColor: '#166beb',
              handleStyle: {
                color: '#166beb',
                borderColor: '#166beb',
                borderWidth: 4
              },
              moveHandleSize: 10,
              moveHandleStyle: {
                color: '#166beb'
              },
              textStyle: {
                color: '#00c6ff'
              },
              emphasis: {
                handleStyle: {
                  color: '#00c6ff',
                  borderColor: '#00c6ff',
                  borderWidth: 4
                },
                moveHandleStyle: {
                  color: '#00c6ff'
                }
              }
            }
          ],
          series: [
            {
              type: ['line', 'line', 'line', 'line', 'line', 'line'],
              connectNulls: true,
              label: {
                show: false
              },
              itemStyle: {
                color: ['#00AFFB', '#FAA51D', '#1da7ff', '#ec6941', '#ffba0a']
              },
              areaStyle: {
                opacity: 0
              }
            },
            {
              data: [9800],
              type: 'pictorialBar', // 顶部
              barMaxWidth: '62',
              symbolPosition: 'end',
              symbolOffset: [34, '-100%'],
              symbolSize: [62, 2],
              symbol: 'rect',
              color: '#fff', // Specify the color using rgb format
              zlevel: 2
            }
          ]
        }
      },
      getData: () => {
        BarState.params.loading = true
        httpRequest
          .get({ url: 'M2P3CpiLine', params: {} })
          .then((res) => {
            if (res.code === 200 && Object.keys(res.map).length > 0) {
              BarState.params.options.dataset.dimensions = [
                'name',
                ...Object.keys(res.map)
              ]
              BarState.unit = res.unit ? `单位：${res.unit}` : ''
              // let dataList: any = []
              // res.data.forEach((item: any) => {
              //   item.value1 && dataList.push(item.value1)
              //   item.value2 && dataList.push(item.value2)
              // })
              // dataList = dataList.filter((item: any) => Number(item) > 0)
              // const dataMin = Math.min(...dataList)
              // BarState.params.options.yAxis[0].min = (dataMin - 10).toFixed(0)
              // BarState.params.options.yAxis[0].name = [res.unit]
              BarState.params.options.dataset.source = res.data
              // BarState.params.options.title.text = res.title
              BarState.title = res.title
              BarState.params.map = res.map
              BarState.params.loading = false
            }
          })
          .catch(() => {
            console.log('请求失败')
          })
      }
    })
    // watch(
    //   () => props.goodsId,
    //   (newVal) => {
    BarState.getData()
    const refreshData = () => {
      BarState.getData()
    }
    //   }
    // )

    const handIconClick = () => {
      emit('enlarge')
    }

    return {
      BarState,
      refreshData,
      handIconClick
    }
  }
})
</script>
<style lang="less" scoped>
.nav {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  padding-left: 5px;
  .echart-item {
    width: 100%;
  }
  .box-bar {
    height: 100%;
    padding: 7px 30px 7px 15px;
  }
}

.item-icon {
  cursor: pointer;
  z-index: 2;
  font-size: 25px;
  margin-top: 12px;
  margin-left: 78px;
  color: #fff;
  &:hover {
    color: rgba(255, 255, 255, 0.5);
  }
  &.rightTop {
    margin-left: 138px;
  }
  &.active {
    color: #00c6ff;
  }
}
</style>
